package bavaria.hightech.proxy;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.InvocationTargetException;
import java.util.logging.Logger;

import bavaria.hightech.banking.IBankClientView;


public class LoggingHandler implements InvocationHandler  {
    private IBankClientView bank;
	private static Logger logger = Logger.getLogger(LoggingHandler.class.getName());
    
    public LoggingHandler(IBankClientView bank)  {
        this.bank = bank;
    }
    
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable{
    	StringBuffer buf = new StringBuffer("* calling method " + method + " with params ");
        for (int i = 0; i < args.length; i++) 
        	buf.append(" " + args[i]);
        buf.append("\n");

        Object result = null;
        try  {
            result = method.invoke(bank, args);
        } catch(IllegalAccessException ex)  {
            ex.printStackTrace();
        } catch(InvocationTargetException ex) {
            buf.append("* exception:" + ex.getTargetException());
            throw ex.getTargetException();
        }

        buf.append("* result:" + result);
        logger.info(buf.toString());
        return result;
    }

}